package com.example.excelimporter.listener;

import com.example.excelimporter.model.ListenerConfig;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class PostImportListenerFactory {
    private final RestTemplate restTemplate;

    public PostImportListenerFactory(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public PostImportListener createListener(ListenerConfig config) {
        switch (config.getType().toUpperCase()) {
            case "HTTP":
                return new HttpPostImportListener(restTemplate, config.getTarget(), config.getConfig());
            case "CODE":
                try {
                    Class<?> listenerClass = Class.forName(config.getTarget());
                    return (PostImportListener) listenerClass.getDeclaredConstructor().newInstance();
                } catch (Exception e) {
                    throw new IllegalArgumentException("Failed to create custom listener: " + e.getMessage(), e);
                }
            default:
                throw new IllegalArgumentException("Unsupported listener type: " + config.getType());
        }
    }
} 